package lumis.service.portalmanagement.audit;

import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.audit.AuditManager;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.clock.ClockConfig;
import lumis.portal.manager.ManagerFactoryInternal;
import lumis.portal.service.GenericServiceClock;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.ITransaction;

/**
 * Clock for audit entries clean up.
 * Reads the time interval for clean up from a custom property and call
 * {@link lumis.portal.audit.IAuditManager} to perform the clean up.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class AuditEntryCleanerClock extends GenericServiceClock
{
	public void doTick(SessionConfig sessionConfig, ClockConfig clockConfig) throws PortalException
	{
		ITransaction transaction = PortalTransactionFactory.createTransaction();
		try
		{
			transaction.begin();

			// call the audit manager to remove the old entries:
			AuditManager auditManager = (AuditManager) ManagerFactoryInternal.getAuditManager();
			auditManager.removeOldEntries();

			transaction.commit();
		}
		catch (PortalException e)
		{
			transaction.rollback();
			throw e;
		}
		catch (Exception e)
		{
			transaction.rollback();
			throw new UnexpectedException(e);
		}
		finally
		{
			transaction.dispose();
		}
	}
}